/**
 * 
 */
package controller;

import java.util.Map;

import org.apache.struts2.interceptor.SessionAware;

import com.opensymphony.xwork2.ActionSupport;
import exception.ApplicationException;

import service.SystemService;
import service.SystemServiceImpl;

import model.Administrator;
import model.User;

/**
 * @ClassName: SystemAction
 * @Description:
 * @author Hongming Wu
 * @date 2013-12-7
 * 
 */
public class SystemAction extends ActionSupport implements SessionAware {

	private Map<String, Object> session;
	private Administrator admin;
	private User user;
	private String userName;
	private String userPwd;
	private String type;

	public Administrator getAdmin() {
		return admin;
	}

	public void setAdmin(Administrator admin) {
		this.admin = admin;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public String getUserPwd() {
		return userPwd;
	}

	public void setUserPwd(String userPwd) {
		this.userPwd = userPwd;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public String gotoLogin() {
		return "loginPage";
	}

	public String login() {
		if (type.equals("Administrator")) {
			return "adminLogin";
		} else {
			return "userLogin";
		}
	}

	public String adminLogin() {

		SystemService ss = new SystemServiceImpl();
		try {
			Administrator detailAdmin = ss.checkAdmin(userName, userPwd);
			session.put("loginedAdmin", detailAdmin);
			if (detailAdmin.getAdmin_Title().equals("senior_admin")) {
				return "senioradminLoginPage";
			}
			if (detailAdmin.getAdmin_Title().equals("security")) {
				return "securityLoginPage";
			}
			System.out.println(detailAdmin.getAdmin_ID() + " logined. ");
		} catch (ApplicationException e) {
			this.addActionError(e.getMessage());
			return "loginPage";
		}
		return null;
	}

	public String userLogin() {
		SystemService ss = new SystemServiceImpl();
		try {
			User detailUser = ss.checkUser(userName, userPwd);
			user = detailUser;
			session.put("loginedUser", detailUser);
		} catch (ApplicationException e) {
			this.addActionError(e.getMessage());
			return "loginPage";
		}
		System.out.println(user.getUser_ID() + " logined. ");
		return "userMainPage";
	}

	public String adminLogout() {
		session.remove("loginedAdmin");
		session = null;
		return "gotoLoginAction";
	}

	public String userLogout() {
		session.remove("loginedUser");
		session = null;
		return "gotoLoginAction";
	}

	@Override
	public void setSession(Map<String, Object> session) {
		this.session = session;

	}

}
